import requests, json
import datetime
import time
############################## 填表接龙
activityId = "2405250517717650"
inviteUid = 139601259
name = "刘莲英"  # 接龙名
Authorization = "eyJhbGciOiJIUzUxMiJ9.eyJ1aWQiOjE4Mjg5MzkyMiwiZXhwIjoxNzE3MzA5MjI0fQ.8GrdbdYvfQTKc2jdnYjTeei5kjLBoaKgK3TqnFIiRqzexeAwNKIGC2vUfHzMhWL4K_Zr_hES5Vi_1yAlC4kLXw"
itemAttrId = 46069628
##############################

while datetime.datetime.now() < datetime.datetime(2024, 5, 26, 15, 0, 0):
    pass


def run(f_w):
    try:
        t1 = time.time()
        # 表格
        goodsUrl = "https://apipro.qunjielong.com/activity-biz/activity/detail/frame/chart"
        data = {
            "activityId": activityId,
            "enterStatus": 1,
            "inviteUid": inviteUid  # 139601259 霍医生
        }

        headers = {
            "Host": "apipro.qunjielong.com",
            "Connection": "keep-alive",
            "device-type": "10",
            "mini-route": "pro/pages/generate-order/generate-order",
            "Authorization": Authorization,
            "uid": "182893922",
            "companyId": "190",
            "scene-flow": "pro/pages/generate-order/generate-order,e74e9033d58c99ee377069c998039a2b",
            "client-version": "5.5.100",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090a13) XWEB/9129",
            "Content-Type": "application/json",
            "xweb_xhr": "1",
            "appid": "wx059cd327295ab444",
            "sceneCode": "1007",
            "version": "3.0.0",
            "Accept": "*/*",
            "Sec-Fetch-Site": "cross-site",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Dest": "empty",
            "Referer": "https://servicewechat.com/wx059cd327295ab444/1784/page-frame.html",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9"
        }

        # res = requests.post(goodsUrl, headers=headers, data=json.dumps(data))
        # print(res.text)
        # js = res.json()
        # actGoodsId = js.get("data").get("activityItemAttr").get("attrItemList")[0].get("actGoodsId")
        # activityItemAttr

        # participator_fill_content = "https://apipro.qunjielong.com/activity-accessory/activity_input/participator_fill_content?actId={}".format(
        #     activityId)
        # res = requests.get(participator_fill_content, headers=headers)
        # print(res.text)
        # js = res.json()
        # return

        data = {
            "chartOrderDTO": {
                "orderDefItemList": [{
                    # "itemAttrId": js.get("data").get("chartActParticipatorFillContentDTO").get("actItemAttrs")[0].get(
                    #     "itemAttrId"),
                    "itemAttrId": itemAttrId,
                    "extensionType": 0,
                    "attrInputDTO": {
                        "type": 10,
                        "textInputDTO": {
                            "text": name
                        }
                    }
                }],
                "markInfoJson": {}
            },
            "actId": activityId
        }
        print(data)
        f_w.write(json.dumps(data) + "\n")
        place_all = "https://apipro.qunjielong.com/order-place/api/mina/order_place/place-all"
        res = requests.post(place_all, headers=headers, data=json.dumps(data))
        print(res.text)
        f_w.write(res.text + "\n")
        js = res.json()
        if js.get("data").get("success"):
            orderNo = js.get("data").get("orderNo")
            # 4. 获取结果 https://apipro.qunjielong.com/order-place/api/mina/order_place/query_order_pay_success_toast_list?actId=2404230269127881&orderNo=240423171385599723874877228
            query_order_pay_success_toast_list = "https://apipro.qunjielong.com/order-place/api/mina/order_place/query_order_pay_success_toast_list?actId={}&orderNo={}".format(
                activityId, orderNo)
            res = requests.get(query_order_pay_success_toast_list, headers=headers)
            return True
        return False

    except Exception as e:
        print(e)
        return False


with open("tb.log", "w") as f_w:
    for i in range(100):
        f_w.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") + "\n")
        if run(f_w):
            break
        time.sleep(0.1)

